package com.bosssoft.boss_exam_web.Controller;

import com.bosssoft.boss_exam_core.enums.ResultCode;
import com.bosssoft.boss_exam_core.exception.QueryDataException;
import com.bosssoft.boss_exam_core.exception.ReflectionException;
import com.bosssoft.boss_exam_core.util.web.ResponseUtil;
import com.bosssoft.boss_exam_db.entity.Admin;
import com.bosssoft.boss_exam_db.entity.Company;
import com.bosssoft.boss_exam_db.service.busines.AdminService;
import com.bosssoft.boss_exam_db.service.busines.CompanyService;
import com.bosssoft.boss_exam_db.service.busines.DashboardService;
import com.bosssoft.boss_exam_web.common.annotation.LoginAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

@RestController
@RequestMapping("/admin/dashboard")
@Validated
@Slf4j
@Api(value="系统面板控制器")
public class DashboardController {

    // 引入面板查询服务
    @Autowired
    private DashboardService dashboardService;

    // 引入管理员服务
    @Autowired
    private AdminService adminService;

    //引入公式服务
    @Autowired
    private CompanyService companyService;


    @ApiOperation(value="查询面板计数器", notes="查询控制台面板的计数器数据" ,httpMethod="GET",response = Object.class)
    @GetMapping
    public Object queryDashboardCouter(@LoginAdmin String  adminId){
        Admin admin = null;
        Object coutShow=null;
        try {
            admin = adminService.selectByKey(adminId);
            Company company = companyService.selectByKey(admin.getCompanyId());
            coutShow = dashboardService.getCoutShow(company);
        } catch (NoSuchMethodException |InvocationTargetException|IllegalAccessException e) {
            log.error("【系统错误】 反射操作异常"+e);
            throw new ReflectionException(ResultCode.SYSTEM_INNER_ERROR);
        } catch (Exception e) {
            log.error("【业务错误】 获取面板计数器异常"+e);
            throw  new QueryDataException();
        }
        return ResponseUtil.ok(coutShow);
    }

}
